﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NetFtp.Core.Utils;
using NetFtp.Core.DAO;

namespace NetFtp.Core.Login
{
    /// <summary>
    /// Une liste d'utilisateur de NetFtp.
    /// </summary>
    public class UserList
    {
        private IList<User> users;
        private UserAccess usersbdd;

        #region Constructeurs
        /// <summary>
        /// Constructeur par défaut
        /// </summary>
        public UserList()
        {
            usersbdd = new UserAccess();
            users = usersbdd.liste;
        }
        #endregion

        /// <summary>
        /// Permets de parcourir tous les utilisateurs.
        /// </summary>
        /// <returns></returns>
        public IEnumerator<User> GetUserEnumerator()
        {
            return users.GetEnumerator();
        }

        /// <summary>
        /// Permets d'ajouter des utilisateurs.
        /// </summary>
        /// <returns></returns>
        public void AddUser(User user)
        {
            if (this.SearchUser(user.Username) == false)
            {
                usersbdd.doSave(user);
                users = usersbdd.liste;
            }
            else
            {
                throw new ApplicationException("Username already exist.");
            }
        }

        /// <summary>
        /// modifier un user
        /// </summary>
        /// <returns></returns>
        public void ChangePwd(User user, string newpass)
        {
            string pass = StringUtils.HashString(newpass);
            
            if (this.SearchUser(user.Username))
            {
                users[users.IndexOf(user)].Password = pass;
                usersbdd.doUpdate(user, pass);
                users = usersbdd.liste;
            }
            else
            {
                throw new ApplicationException("User not exist.");
            }
        }

         /// <summary>
         /// Permets de chercher des utilisateurs.
         /// </summary>
         /// <returns>
         /// true si trouvé flase sinon
         /// </returns>
         public bool SearchUser(string username)
         {
             foreach (User usertmp in users)
             {
                 if (usertmp.Username == username)
                 {
                     return true;
                 }
             }
             return false;
         }

         /// <summary>
         /// Permets de chercher des utilisateurs+password.
         /// </summary>
         /// <returns>
         /// true si trouvé flase sinon
         /// </returns>
         public bool SearchUser(string username, string password)
         {
             string pass = StringUtils.HashString(StringUtils.HashString(password));

             foreach (User usertmp in users)
             {
                 if ((usertmp.Username == username) && (usertmp.Password == pass))
                 {
                     return true;
                 }
             }
             return false;
         }

         /// <summary>
         /// Retourne le user ayant ce login et mot de passe
         /// </summary>
         public User ReturnUser(string username)
         {
             foreach (User usertmp in users)
             {
                 if (usertmp.Username == username)
                 {
                     return usertmp;
                 }
             }
             return null;
         }
    }
}
